package com.yunshang.shoppingmall.controller.product;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yunshang.shoppingmall.common.MessageConstants;
import com.yunshang.shoppingmall.common.Response;
import com.yunshang.shoppingmall.common.UploadImgs;
import com.yunshang.shoppingmall.entity.*;
import com.yunshang.shoppingmall.service.*;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.math.BigDecimal;

import java.sql.Timestamp;
import java.util.*;


/**
 * 产品管理
 */
@Controller
public class ProductController {
    @Autowired
    ProductService productService;
    @Autowired
    ProductPriceService productPriceService;
    @Autowired
    ProductGuiGeService productGuiGeService;
    @Autowired
    ProductAllsService productAllsService;
    @Autowired
    ClassifyService classifyService;
  List imgList=new ArrayList();
    List guigeImgGroup=new ArrayList();
    private String imgGroup=null;
    private String productProImg;//商品显示图片
    private String imagePath=null;
    private  String productDetailContent=null;
    private String productDetailContentImgPath=null;//详情图路径
    private String guigeImg=null;//规格图
    private String classfyImg=null;//分类图
    //进入产品列表页面
    @RequestMapping("/inProductList")
    public String inProductList(){
        return "product/productList";
    }

    /**
     * 查询商品列表
     * @return
     */
    @RequestMapping("/inProductLists")
    public ModelAndView inProductLists(HttpServletRequest request){
            String pageNum=request.getParameter("pageNum");
            int pageNums=1;
            if(pageNum!=""&&pageNum!=null){
                pageNums=Integer.parseInt(pageNum);
            }
            Map<String,Object> map =new HashMap<>();
            ModelAndView modelAndView=new ModelAndView("product/productLists");
            PageHelper.startPage(pageNums,10);
            List<ProductAlls> productAllsList= productAllsService.selectProductAlls();
            PageInfo pageInfo = new PageInfo(productAllsList);
            modelAndView.addObject("proList",pageInfo.getList());
            modelAndView.addObject("pages",pageInfo.getPages());
            modelAndView.addObject("currentPage",pageInfo.getPageNum());
            return modelAndView;
    }
    @ResponseBody
    @RequestMapping("/searchProductByName")
    public ModelAndView  searchProductByName(String productName){
        ModelAndView modelAndView=new ModelAndView("product/productLists");
        List<ProductAlls> productAllsList= productAllsService.selectByProductName(productName);
        return modelAndView;
    }


    //增加产品页面
    @RequestMapping("/addProduct")
    public String  addProduct(){
     Logger logger= LoggerFactory.getLogger(ProductController.class);
        return "product/addProduct";
    }

    /**
     * 增加商品数据
     *
     */
    @ResponseBody
    @RequestMapping(value = "/addProductData",method = RequestMethod.POST)
    public Map<String,Object>  addProductData(@RequestParam Map<String,String> proMap){
    Map<String,Object> map=new HashMap<>();
        //封装product类
        Product product=new Product();
        product.setProductName(proMap.get("productName"));
        product.setProductDesc(proMap.get("productDesc"));
        product.setProductUnit(proMap.get("productUnit"));
        product.setSupplier(proMap.get("supplier"));
        System.out.println("classId:"+proMap.get("classId"));
            int classIds=Integer.parseInt(proMap.get("classId"));
             product.setClassId(classIds);

        product.setProductNo(proMap.get("productNo"));
        product.setProductseries(proMap.get("productSeries"));
        product.setProductshortname(proMap.get("productShortName"));
        product.setActiveSign(Integer.parseInt(proMap.get("activeSign")));
        System.out.println("activeSign:------>"+Integer.parseInt(proMap.get("activeSign")));
            Timestamp upTime=new Timestamp(System.currentTimeMillis());

            product.setUptime(upTime);
            int sort=0;
        product.setSort(sort);

      product.setProductUpState(proMap.get("productUp"));

        //保存商品详情图片的url
        product.setDetailContent(productDetailContentImgPath);

        //保存商品主图
        product.setProImg(imagePath);
        //调用添加产品方法
        int rows=productService.insertSelective(product);
        System.out.println("rows:"+rows);
        int productId=product.getProductId();
        productDetailContentImgPath=null;
        if(productId!=0){
            map.put("productId",productId);
        }
        return map;
    }

   //增加商品价格
    @ResponseBody
    @RequestMapping("/addProductPrice")
   public String  addProductPrice( @RequestParam("productNo") String productNo,@RequestParam("proPrice") String  proPrice,@RequestParam("proMarketPrice") String proMarketPrice,
                                   @RequestParam("proCost") String  proCost,@RequestParam("opratorCost") String  opratorCost,@RequestParam("coupon") String coupon,
                                   @RequestParam("huiYuan") String huiYuan,@RequestParam("dianZhu") String  dianZhu, @RequestParam("YZDianZhu") String  YZDianZhu,
                                   @RequestParam("visitor") String visitor,@RequestParam("otherRank") String otherRank,@RequestParam("guigeId") String guigeId,
                                   @RequestParam("productId") String productId){
        ProductPrice productPrice=new ProductPrice();
        productPrice.setProductNo(productNo);
        if(proPrice!=""&&proPrice!=null) {
            BigDecimal proPrices = new BigDecimal(proPrice);
            productPrice.setProPrice(proPrices);
        }
        if(proMarketPrice!=""&&proMarketPrice!=null){
            BigDecimal proMarketPrics=new BigDecimal(proMarketPrice);
            productPrice.setMarketprice(proMarketPrics);
        }

        if(proCost!=""&&proCost!=null){
            BigDecimal proCosts=new BigDecimal(proCost);
            productPrice.setProductCost(proCosts);
                }
        if(opratorCost!=null&&opratorCost!=""){
            BigDecimal opratorCosts=new BigDecimal(opratorCost);
            productPrice.setOpratorCost(opratorCosts);
        }
        if(coupon!=null&&coupon!=""){
            BigDecimal coupons=new BigDecimal(coupon);
            productPrice.setCoupon(coupons);
        }
        //游客
      if(visitor!=""&&visitor!=null){
           int visitors= Integer.parseInt(visitor);
           productPrice.setVisitor(visitors);
      }
        if(huiYuan!=""&&huiYuan!=null){
            int huiyuans=Integer.parseInt(huiYuan);
            productPrice.setHuiyuan(huiyuans);
        }

        if(dianZhu!=""&&dianZhu!=null){
            int dianzhus=Integer.parseInt(dianZhu);
            productPrice.setDianzhu(dianzhus);
        }

        if(YZDianZhu!=""&&YZDianZhu!=null){
            int YZDianZhus=Integer.parseInt(YZDianZhu);
            productPrice.setYzDianzhu(YZDianZhus);
        }
            //其他等级
        if(otherRank!=""&&otherRank!=null){
            int otherRanks=Integer.parseInt(otherRank);
            productPrice.setOtherRank(otherRanks);
        }

        if(guigeId!=""&&guigeId!=null){
            int guigeIds=Integer.parseInt(guigeId);
            productPrice.setGuigeId(guigeIds);
        }

        if(productId!=""&&productId!=null){
            int productIds  =Integer.parseInt(productId);
            productPrice.setProductId(productIds);
        }
        //保存数据
      int rows=  productPriceService.insert(productPrice);
        if(rows>0){
            return "addSuccess";
        }
        return "addFaile";
   }


    //商品图片上传接口
    @ResponseBody
    @RequestMapping(value = "/addProductImgs" ,method = RequestMethod.POST)
    public Map<String,Object> uploadImgs(MultipartFile file,HttpServletRequest request){
        Map<String,Object> map=new HashMap<>();
        map=UploadImgs.uploads(file);
        imagePath=(String) map.get("imagePath");
        System.out.println("imagePath:"+imagePath);
        return map ;
    }

//上传规格图片接口
@ResponseBody
@RequestMapping(value = "/addGuiGeImg" ,method = RequestMethod.POST)
public Map<String,Object> addGuiGeImg(MultipartFile file,HttpServletRequest request){
    Map<String,Object> map=new HashMap<>();
    map=UploadImgs.uploads(file);
    guigeImg=(String) map.get("imagePath");
    System.out.println("imagePath:"+guigeImg);
    if(guigeImg!=null&&guigeImg!=""){
        map.put("msg","1");
    }

    return map ;
}

//上传规格图组接口
@ResponseBody
@RequestMapping(value = "/addGuiGeImgGroup" ,method = RequestMethod.POST)
public Map<String,Object> addGuiGeImgGroup(MultipartFile file,HttpServletRequest request){
    Map<String,Object> map=new HashMap<>();
    map=UploadImgs.uploads(file);
    imgGroup=(String) map.get("imagePath");
    guigeImgGroup.add(imgGroup);
    System.out.println("imagePath:"+imgGroup);

    return map ;
}



//上传详情图片接口
@ResponseBody
@RequestMapping(value = "/addproDetailImgs" ,method = RequestMethod.POST)
public Map<String,Object> addproDetailImgs(MultipartFile file,HttpServletRequest request){
    Map<String,Object> map=new HashMap<>();
    map=UploadImgs.uploads(file);
    productDetailContentImgPath=(String) map.get("imagePath");
    System.out.println("imagePath:"+productDetailContentImgPath);
    return map ;
}

//


//添加规格
@ResponseBody
@RequestMapping("/addProductGuige")
public  Map<String,Object> addProductGuige(@RequestParam("productNo") String productNo, @RequestParam("proSize") String proSize, @RequestParam("proWeight") String proWeight,
                              @RequestParam("proTiji") String proTiji, @RequestParam("proColor") String proColor, @RequestParam("otherGuige") String otherGuige,
                             @RequestParam("proLength") String proLength, @RequestParam("proWidth") String proWidth, @RequestParam("proHeight") String proHeight,
                             @RequestParam("productId") String productId,@RequestParam("proVolume") String proVolume,@RequestParam("guigeName") String guigeName,
                             @RequestParam("buyCount")String buyCount,@RequestParam("buyCountTotal")String buyCountTotal,@RequestParam("buySign") String buySign,
                              @RequestParam("commissionSign") String commissionSign){
    System.out.println("--buySign-->"+buySign+"---buycount-->"+buyCount+"--buyTotal-->"+buyCountTotal+"----commissign--->"+commissionSign);
    Map<String,Object> map=new HashMap<>();
    ProductGuiGe productGuiGe=new ProductGuiGe();
    productGuiGe.setProductNo(productNo);
    if(proSize!=null&&proSize!=""){
      Double proSizes=  Double.parseDouble(proSize);
        productGuiGe.setProSize(proSizes);
    }
    if(proWeight!=null&&proWeight!=""){
        Double proWeights=  Double.parseDouble(proWeight);
        productGuiGe.setProWeight(proWeights);
    }
   if(proTiji!=null&&proTiji!=""){
       Double proTijis=  Double.parseDouble(proTiji);
       productGuiGe.setProTiji(proTijis);
   }
   int buyCounts=0;
   if(buyCount!=null&&buyCount!=""){
       buyCounts=Integer.parseInt(buyCount);
   }
   productGuiGe.setBuyCount(buyCounts);
   int buyCountTotals=0;
   if(buyCountTotal!=null&&buyCountTotal!=""){
       buyCountTotals=Integer.parseInt(buyCountTotal);
   }
    productGuiGe.setBuyCountTotal(buyCountTotals);
    productGuiGe.setBuySign(buySign);
    productGuiGe.setCommissionSign(commissionSign);


   //规格名称
    productGuiGe.setGuigeName(guigeName);
    productGuiGe.setProColor(proColor);
    productGuiGe.setProOther(otherGuige);
    productGuiGe.setImgurl(guigeImg);//规格图


    //拼接字符串
    String imagesPath="";
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i <guigeImgGroup.size() ; i++) {
        if(guigeImgGroup.get(i)!=null&&guigeImgGroup.get(i)!=""){
            if(guigeImgGroup.get(i)!=guigeImgGroup.get(i+1)){
                sb.append(guigeImgGroup.get(i)+",");
            }
        }
    }
    imagesPath=sb.toString();
    //封装图组
  productGuiGe.setImgGroup(imagesPath);
    imagesPath=null;
    guigeImgGroup=null;
  if(productId!=""&&productId!=null){//商品id
      int productIds=Integer.parseInt(productId);
      productGuiGe.setProductId(productIds);
  }
 if(proWidth!=""&&proWidth!=null){
      double proWidths=Double.parseDouble(proWidth);
      productGuiGe.setProWidth(proWidths);
 }
 if(proHeight!=null&&proHeight!=""){
     double proHeights=Double.parseDouble(proHeight);
     productGuiGe.setProHeight(proHeights);
 }
if(proLength!=null&&proLength!=""){
    double proLengths=Double.parseDouble(proLength);
    productGuiGe.setProLength(proLengths);
}

if(proVolume!=null&&proVolume!=""){
    double proVolumes=Double.parseDouble(proVolume);
    productGuiGe.setProVolume(proVolumes);
}


    //调用保存接口
    int GuigeRow=productGuiGeService.insert(productGuiGe);
    int guigeId=productGuiGe.getGuigeId();

    if(guigeId!=0){
        map.put("guigeId",guigeId);
    }
     return  map;
}

    /**
     * 删除一个产品
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/delProduct")
    public Response delProductById(HttpServletRequest request)throws  Exception{
        //获取页面参数
        String productId=request.getParameter("productId");
         int productIds=Integer.valueOf(productId);

        //创建Map集合
        Map<String,Object> map=new HashMap<>();

         //调用删除方法
        int row=productService.deleteByPrimaryKey(productIds);
        map.put("rows",row);
        return new Response(MessageConstants.MSG_SUCCESS,map,"");
    }

    /**
     * 更新产品
     * @return
     */
    @ResponseBody
    @RequestMapping("/updateProduct")
    public Response updateProduct(HttpServletRequest request)throws  Exception{

        //获取页面表单参数
        String productName=request.getParameter("productName");
        String repId=request.getParameter("repId");
        String productDesc=request.getParameter("proiductDesc");
        String productUnit=request.getParameter("productUnit");
        String supplier=request.getParameter("supplier");//供货商

        //创建Map集合
        Map<String,Object> map=new HashMap<>();

        //创建产品对象
        Product product=new Product();
        product.setProductName(productName);
        product.setProductDesc(productDesc);
        product.setProductUnit(productUnit);
        product.setSupplier(supplier);

        //调用更新方法
        int rows=productService.updateByPrimaryKeySelective(product);

        map.put("rows",rows);

        return new Response(MessageConstants.MSG_SUCCESS,map,"");
    }

    /**
     * 根据产品名称查询产品
     * @return
     */
    @ResponseBody
    @RequestMapping("/selectProductByProductName")
    public Response selectProductByProductName(HttpServletRequest request)throws Exception{
        //获取页面参数
        String productName=request.getParameter("productName");

        List<Product> productList=new ArrayList<>();
        //调用查询方法
//
//        Page page=PageHelper.startPage(1,50,true);
//       productList=productService



        //创建Map集合
        Map<String,Object> map=new HashMap<>();
        map.put("productList",productList);

        return  new Response(MessageConstants.MSG_SUCCESS,map,"");
    }

    //产品列表的页面
    @RequestMapping("/getProductList")
    public ModelAndView getProductList(){
        ModelAndView modelAndView=new ModelAndView("product/productList");
        //查询产品列表
         PageHelper.startPage(1,5);
//       List<Product> productList =productService.selectProductList();
//       PageInfo pageInfo=new PageInfo<>(productList);
//        System.out.println("=====>"+productList);
//        modelAndView.addObject("productList",productList);
        return modelAndView;
    }

//增加商品分类图片
    @ResponseBody
@RequestMapping("/addProductClassfyImgs")
public ModelAndView addProductClassfyImgs(){
        ModelAndView modelAndView=new ModelAndView("product/classfyImg");
        return modelAndView;
    }

//上传分类图接口

@ResponseBody
@RequestMapping(value = "/addClassfyImgs" ,method = RequestMethod.POST)
public Map<String,Object> addClassfyImg(MultipartFile file,HttpServletRequest request){
    Map<String,Object> map=new HashMap<>();
    map=UploadImgs.uploads(file);
    classfyImg=(String) map.get("imagePath");
    System.out.println("imagePath:"+classfyImg);
    if(classfyImg!=null&&classfyImg!=""){
        map.put("msg","1");
    }
    return map ;
}
@ResponseBody
@RequestMapping(value = "/updateClassfyImgs" ,method = RequestMethod.POST)
public Map<String,Object> updateClassfyImg(@RequestParam Map<String,String> classfyMap){
    Map<String,Object> map=new HashMap<>();
    Classify classify=new Classify();

    if(classfyMap!=null){
        classify.setClassId(Integer.parseInt(classfyMap.get("classId")));
    }
  classify.setRjClassifyImg(classfyImg);
    int row=classifyService.updateByPrimaryKeySelective(classify);
    if(row>0){
        map.put("msg","1");
    }

    return map ;
}

}
